<?php

/*
 * status 0为待确认
 * status 1为已确认
 * status 2为部分退货
 * status 3为部分退货
 * */

class Member_account_logModel extends Model
{
    //列表
    public function _list($view, $map, $sort = 'sort', $pagesize = 20){
        //计算总页数
        $count = $this->where($map)->count();
        //实例化分页类
        import('ORG.Util.Page');
        $p = new Page($count, $pagesize);
        $list = $this->where($map)->order($sort)->limit($p->firstRow . ',
' . $p->listRows)->select();
        foreach($list as $k =>$v){
            $list[$k]['ordernumber']=D('Order')->where('id = '.$v['order_id'])->getfield('ordernumber');
            //$list[$k]['total']=D('Order')->where('id = '.$v['order_id'])->getfield('total');
        }
        $pages = $p->show();
        $view->assign('list', $list);
        $view->assign('pages', $pages);
    }

    //列表
    public function _list2($view, $map, $sort = 'sort', $pagesize = 20){
        //计算总页数
        $count = $this->alias('i')->where($map)->join('__ORDER__ o on o.id = i.order_id')->count();
        //实例化分页类
        import('ORG.Util.Page');
        $p = new Page($count, $pagesize);
        $list = $this->alias('i')->where($map)->join('__ORDER__ o on o.id = i.order_id ')->order($sort)->limit($p->firstRow . ',
' . $p->listRows)->field('i.*,o.ordernumber as ordernumber,o.total as total')->select();
//        foreach($list as $k =>$v){
//            $list[$k]['ordernumber']=D('Order')->where('id = '.$v['order_id'])->getfield('ordernumber');
//            $list[$k]['total']=D('Order')->where('id = '.$v['order_id'])->getfield('total');
//        }
        $pages = $p->show();
        $view->assign('list', $list);
        $view->assign('pages', $pages);
    }

    //列表
    public function _listgroup($view, $map, $sort = 'sort', $pagesize = 20)
    {
        //计算总页数
        $count = M() -> query("select count(*) as tp from ".$this->where($map)->group('order_id,product_id,attr')->buildSql().' as a');
		$count = $count[0]['tp'];
        //实例化分页类
        import('ORG.Util.Page');
        $p = new Page($count, $pagesize);
        $list = $this->where($map)->order($sort)->limit($p->firstRow . ',
' . $p->listRows)->group('order_id,product_id,attr')->Field('sum(changenum) as changenum, order_sn, dateline, status, order_id, attr, product_id')->select();
        $pages = $p->show();
        foreach ($list as $k => $v) {

            $list[$k]['ostatus'] = M('order')->where(array('id' => $v['order_id']))->getField('status');
        }
        $view->assign('list', $list);
        $view->assign('pages', $pages);
    }

    //更新日志表信息
    public function up_account_log($member_id, $data)
    {
        $data['member_id'] = $member_id;
        $mes = $this->add($data);
        if ($mes) {
            return true;
        } else {
            return false;
        }

    }


    //改变用户余额
    public function change($num = 0, $member_id = 0, $remark = '', $pdstate = 0)
    {
		
		if($pdstate==0){
        if ($num == 0) {
            return false;
        }
        $memberDb = D('Member');
        //微信通知
		$wxuser = $memberDb->where(array('id' => $member_id))->find();
		if(!$wxuser){
			return false;
		}
        
		if ($num > 0) {
			$text = "尊敬的" . $wxuser['nickname'] . ":\r\n您的账户资金增加" . abs($num) . "\r\n原因：" . $remark . "\r\n目前余额为：" . ($wxuser['money']*1+$num);
		} else {
			$text = "尊敬的" . $wxuser['nickname'] . ":\r\n您的账户资金减少" . abs($num) . "\r\n原因：" . $remark . "\r\n目前余额为：" . ($wxuser['money']*1+$num);
		}
		//sendWXMsg($wxuser['openid'], $text);


        $data['user_id'] = 0;
        $data['member_id'] = $member_id;
        $data['remark'] = $remark;
		$data['site_id'] = $wxuser['site_id'];
		$data['pdstate'] = $pdstate;
        $data['changenum'] = $num;
        $data['dateline'] = time();

        $id = $this->add($data);
		if($id){
			
            $ret = $memberDb->where(array('id' => $member_id))->setInc('money', $num);
			if($ret !== false){
				
				return true;
			}else{
				$this->where(array('id' => $id)) -> delete();
				return false;
			}
		}else{
			return false;
		}
		}else if($pdstate==3){
			   if ($num == 0) {
            return false;
        }
        $memberDb = D('Member');
        //微信通知
		$wxuser = $memberDb->where(array('id' => $member_id))->find();
		if(!$wxuser){
			return false;
		}
        
		if ($num > 0) {
			$text = "尊敬的" . $wxuser['nickname'] . ":\r\n您的账户积分增加" . abs($num) . "\r\n原因：" . $remark . "\r\n目前余额为：" . ($wxuser['money']*1+$num);
		} else {
			$text = "尊敬的" . $wxuser['nickname'] . ":\r\n您的账户积分减少" . abs($num) . "\r\n原因：" . $remark . "\r\n目前余额为：" . ($wxuser['money']*1+$num);
		}
		//sendWXMsg($wxuser['openid'], $text);


        $data['user_id'] = 0;
        $data['member_id'] = $member_id;
        $data['remark'] = $remark;
		$data['site_id'] = $wxuser['site_id'];
		$data['pdstate'] = $pdstate;
        $data['changenum'] = $num;
        $data['dateline'] = time();

        $id = M('Integral_log')->add($data);
		if($id){
			
            $ret = $memberDb->where(array('id' => $member_id))->setInc('offline', $num);
			if($ret !== false){
				
				return true;
			}else{
				$this->where(array('id' => $id)) -> delete();
				return false;
			}
		}else{
			return false;
		}
			}
    }


    //确认一个订单分成日志
    public function confirm($order_id, $product_id = 0, $attr = '')
    {
        $map['order_id'] = $order_id;
        if ($product_id > 0) {//产品id号
            $map['product_id'] = $product_id;
        }
        if ($attr != '') {$map['attr'] = $attr;}
		$map['status'] = 0;//订单状态
		$sale=M('Sale')->where(array('order_id'=>$order_id))->select();
		/*foreach($sale as $k => $v){
			if($v['status']!=1||$v['confirm']!=1){
				continue;
				}
		if(!$sale_id){
		$sale_id=$v['id'];	
		}else{
		$sale_id.=','.$v['id'];	
		}
		}
		$map['sale_id']=array('in',$sale_id);*/
        $ret = $this->where($map)->data(array('status' => 1))->save();//写入member_account_log表
		
        if ($ret > 0) {
			$map['status'] = 1; 
            $memberDb = D('Member');
            $member_id = $this->where($map)->select();
            foreach ($member_id as $k => $v) {
                $change=$memberDb->where(array('id' => $v['member_id']))->setInc('money', $v['changenum']);//金额写进会员账户（member表）
				if($change){
				$member=$memberDb->where(array('id' => $v['member_id']))->find();
				$map['member_id']=$v['member_id'];
				$this->where($map)->save(array('total'=>$member['money']));	
				}
                
            }
            return true;
        } else {
            return false;
        }
    }


    //取消一个订单分成日志
    public function cancel($order_id, $product_id = 0, $attr = '', $num = 1)
    {
        $map['order_id'] = $order_id;
        if ($product_id > 0) {
            $map['product_id'] = $product_id;
        }
        if ($attr != '') {
            $map['attr'] = $attr;
        }
        $malog = $this->where($map)->find();
        $data['changenum'] = $malog['changenum'] - ($malog['changenum'] / $malog['num']) * $num;
		
		$data['goodsMoney'] = $malog['goodsMoney'] - ($malog['goodsMoney'] / $malog['num']) * $num;
		
        $data['num'] = $malog['num'] - $num;
        if ($data['num'] > 0) {
            //部分退货
            $data['status'] = 2;
        } else {
            //全部退货
            $data['status'] = 3;
        }
        if ($data['num'] >= 0) {
            $ret = $this->where($map)->data($data)->save();
        }
        if ($ret !== false) {
            return true;
        } else {
            return false;
        }
    }
}
